package com.epro.dx.activity.coupon;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Window;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.activity.LoginActivity;
import com.epro.dx.activity.coupon.fragment.CouponMarketFragment;
import com.epro.dx.activity.coupon.fragment.MyCouponFragment;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.widget.PagerSlidingTabStrip;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.HitBuilders.ScreenViewBuilder;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.ServerConstant;

/**
 * @author Tony 描述：Coupon页面
 */
public class CouponActivity extends BaseFragmentActivity {

	private PagerSlidingTabStrip tab;
	private ViewPager vp_container;
	private String[] tabs = { "CouponMarket", "MyCoupon" };
	private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
	private int currentPage;// 当前要显示的页面标识：0为couponMarket、1为MyCoupon
	private CouponMarketFragment couponMarketFragment;
	private MyCouponFragment myCouponFragment;
	private boolean isMyCoupon;
	private Tracker tracker;
	private String SCREENNAME_MYCOUPON = "MyCoupon";
	private String SCREENNAME_COUPONMARKET = "CouponMarket";
	private ScreenViewBuilder screenViewBuilder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tracker = ((DXApplication)getApplication()).getTracker(TrackerName.APP_TRACKER);
		screenViewBuilder = new HitBuilders.ScreenViewBuilder();
		initView();
		initData();
		initEvent();
	}

	@Override
	protected void onResume() {
		if (isMyCoupon) {
			if (ActivityUtil.isLogin(getApplicationContext())) {
//				// 已经登陆，刷新列表数据
				myCouponFragment.setCurrentPage(1);
				myCouponFragment.pageIndex = 0;
				myCouponFragment.initData();
			} else {
				vp_container.setCurrentItem(0);
			}
		}
		//设置跟踪屏幕名称
		if(currentPage == 0){
			tracker.setScreenName(SCREENNAME_COUPONMARKET);
		}else{
			tracker.setScreenName(SCREENNAME_MYCOUPON);
		}
		tracker.send(screenViewBuilder.build());
		super.onResume();
	}

	/**
	 * 加载事件
	 */
	private void initEvent() {
		// 页面切换事件监听
		tab.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				if (position == 1) {
					// 当切换到MyCoupon页面时判断是否登陆
					if (!ActivityUtil.isLogin(getApplicationContext())) {
						// 没有登陆跳转到登陆页面，登陆成功后返回当前页面
						Intent intent = new Intent(CouponActivity.this,
								LoginActivity.class);
						intent.putExtra(
								ServerConstant.Parameter.PARAMETER_NAME_LOGIN_ACTION_TYPE,
								ServerConstant.Parameter.PARAMETER_ACTION_TYPE_MYCOUPON);
						isMyCoupon = true;
						startActivity(intent);
						return;
					} else {
						tracker.setScreenName(SCREENNAME_MYCOUPON);
						// 已经登陆，刷新列表数据
						myCouponFragment.setCurrentPage(position);
						myCouponFragment.pageIndex = 0;
						myCouponFragment.initData();
					}
				}
				if (position == 0) {
					tracker.setScreenName(SCREENNAME_COUPONMARKET);
					// 切换到CouponMarket页面，刷新数据
					couponMarketFragment.setCurrentPage(position);
					couponMarketFragment.pageIndex = 0;
					couponMarketFragment.initData(true);
				}
				tracker.send(screenViewBuilder.build());
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 加载数据
	 */
	private void initData() {
		currentPage = getIntent().getIntExtra("pageNumber", 0);

		couponMarketFragment = new CouponMarketFragment(currentPage);
		myCouponFragment = new MyCouponFragment(currentPage);
		fragments.add(couponMarketFragment);
		fragments.add(myCouponFragment);

		vp_container.setAdapter(new ViewPagerAdapter(
				getSupportFragmentManager()));
		tab.setViewPager(vp_container);
		tab.setTextColor(
				getResources().getColor(R.color.myorder_tab_text_normal),
				getResources().getColor(R.color.myorder_tab_text_selected));
		vp_container.setCurrentItem(currentPage);
	}

	/**
	 * 加载视图显示
	 */
	private void initView() {
		// 隐藏默认标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置要显示的布局
		setContentView(R.layout.activity_coupon);
		// 设置头部标题以及按钮显示
		setTitleContent("Coupon");
		setLeftBtnContent(true, -1);
		// 查找用到的布局中的控件
		tab = (PagerSlidingTabStrip) findViewById(R.id.indicator_coupon_tab);
		vp_container = (ViewPager) findViewById(R.id.vp_coupon);
	}

	class ViewPagerAdapter extends FragmentPagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return tabs.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			if (tabs != null)
				return tabs[position];
			return super.getPageTitle(position);
		}

	}
}
